Repository navigation
Conversation
|
There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The change removes the 9998 FP from the example in the description, and the tests pass. Two closely related forms of the same loop still give a false positive:
- A flag set at the last valid index:
if (i == 9) done = 1;witha[10]. Base reported index 9998. The PR now reportsarrayIndexOutOfBoundsCondat index 10, which is still wrong:i == 9ends the loop, soinever reaches 10. The value 10 already existed in base, where the 9998 error at the same spot hid it. - A flag combined with a bound in the condition:
!done && i < 100. The condition becomes false within the iteration limit (result == 0), so the last iteration is still taken from the header only, even though the body changesdone. Index 99 is reported fors[4], as in base.
So the reasoning that the last iteration is unknown when the body changes the condition is only applied when the limit is reached. I found no crash, hang or new kind of false positive.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR should have a trac ticket and the title should reference it | @dmcppcheck | general | |
| 2 | Last iteration still taken from the header when the condition ends in time but the body changes it (!done && i < 100) |
AI review | lib/valueflow.cpp:5305 |
🆕 New |
| 3 | Same pattern with the flag set at the last valid index still gives an FP (arrayIndexOutOfBoundsCond index 10) | AI review | test/testbufferoverrun.cpp:2723 |
🆕 New |
@APX403, please fix, resolve or reply to the open and new comments: #1, #2, #3.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestBufferOverrun TestValueFlow TestUninitVar TestNullPointer TestCondition TestOther: 846 tests, 0 failures.
The example in the PR description: base reports index 9998, the PR reports nothing. An endless loop without a flag still reports 9998.
Remaining false positives (/work/{base,pr}/cppcheck -q --enable=warning b.c):
void g1(void) {
int a[10];
for (int i = 0, done = 0; done == 0; i++) {
a[i] = 0;
if (i == 9) done = 1;
}
}
int g3(void) {
const char s[] = "abc";
int n = 0;
for (int i = 0, done = 0; !done && i < 100; i++) {
if (s[i] == '\0') done = 1; else n++;
}
return n;
}base:
b.c:4:10: error: Array 'a[10]' accessed at index 9998, which is out of bounds. [arrayIndexOutOfBounds]
b.c:11:14: error: Array 's[4]' accessed at index 99, which is out of bounds. [arrayIndexOutOfBounds]
pr:
b.c:4:10: warning: Either the condition 'i==9' is redundant or the array 'a[10]' is accessed at index 10, which is out of bounds. [arrayIndexOutOfBoundsCond]
b.c:11:14: error: Array 's[4]' accessed at index 99, which is out of bounds. [arrayIndexOutOfBounds]
In both cases the code is safe:
- In
g1,--debugshows thatihas the possible value 10 in base too (i possible {9,10,0,9998}); only 9998 is gone in the PR. - In
g3, the loop stops ati == 3.
Automated review of bec10bb by Claude. The pull request was built and tested locally.
| // When the limit of iterations was reached because only the loop body changes the condition, | ||
| // the last iteration is not known either | ||
| const Token* bodyStart = tok->linkAt(1)->next(); | ||
| if (result == 0 || !findExpressionChanged(secondExpression, bodyStart, bodyStart->link(), settings)) |
There was a problem hiding this comment.
AI review
The check is skipped when result == 0, but the comment's reasoning applies there too: if the body changes the condition, the loop can end before the header-only simulation does. Example:
const char s[] = "abc";
for (int i = 0, done = 0; !done && i < 100; i++) {
if (s[i] == '\0') done = 1;
}This still gives Array 's[4]' accessed at index 99 (same as base). Dropping memory2 whenever findExpressionChanged(secondExpression, ...) is true, whatever result is, would also cover this case. memoryAfter could still be kept when result == 0.
| " }\n" | ||
| " return n;\n" | ||
| "}\n"); | ||
| ASSERT_EQUALS("", errout_str()); |
There was a problem hiding this comment.
AI review
A variant of the new test is still reported:
void g1(void) {
int a[10];
for (int i = 0, done = 0; done == 0; i++) {
a[i] = 0;
if (i == 9) done = 1;
}
}The PR reports Either the condition 'i==9' is redundant or the array 'a[10]' is accessed at index 10 (base reported index 9998 instead). i == 9 ends the loop, so 10 is never used as an index. The value comes from forwarding i==9 through i++ back into the body, ignoring that done = 1 ends the loop. In base it was hidden behind the 9998 error. Please consider adding this case, at least as a TODO assert.
arrayIndexOutOfBoundsreports index 9998 for aforloop whose condition only changes in its body:valueFlowForLoop2()runs the three expressions of the loop header without the body, sodone == 0stays true untilmaxForLoopCountis reached, and the memory of the last pass is then used as the last iteration, which puts 9998 intoiin the body. Since #8899 the state after the loop is not used when the limit is reached; when the body changes the condition, the last iteration is not known either. An endless loop such asfor (int n = 0; 1; ++n)still reports index 9998, as the test for ticket 13109 expects.raylib's
raygui.hsplits the item texts ofGuiToggleGroup()with such a loop (raygui.h#L2189), which showed up in the package run of #9078.I don't have a Trac account, so I couldn't create a ticket for this.